home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / misc / low-leve / ps2mouse.c < prev    next >
Text File  |  1996-11-17  |  5KB  |  206 lines

  1. Article 13781 of comp.os.linux:
  2. Newsgroups: comp.os.linux
  3. Path: samba!concert!gatech!swrinde!cs.utexas.edu!uunet!mcsun!news.funet.fi!ajk.tele.fi!funic!nntp.hut.fi!nntp!jem
  4. From: jem@snakemail.hut.fi (Johan Myreen)
  5. Subject: Re: 0.98pl1 doesn't recognise my mouse
  6. In-Reply-To: mrd@ecs.soton.ac.uk's message of 20 Oct 92 10:38:36 GMT
  7. Message-ID: <JEM.92Oct20144025@lk-hp-6.hut.fi>
  8. Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
  9. Nntp-Posting-Host: lk-hp-6.hut.fi
  10. Organization: Helsinki University of Technology, Finland
  11. References: <13143@ecs.soton.ac.uk>
  12. Date: 20 Oct 92 12:40:25 GMT
  13. Lines: 189
  14.  
  15. In article <13143@ecs.soton.ac.uk> mrd@ecs.soton.ac.uk (Mark Dobie) writes:
  16.  
  17. >I have a Viglen machine with a PS/2 style mouse. At boot up Linux 0.98
  18. >SLS fails to recognise it as any of the mouse types.
  19.  
  20. Please upgrade to 0.98PL2, or patch your kernel with the patches that
  21. were posted some time ago in comp.os.linux. The PS/2 mouse recognition
  22. doesn't work on some machines running 0.97-0.98PL1.
  23.  
  24. >hlu's 0.97pl6 root disk used to recognise it as a bus mouse.
  25.  
  26. It probably didn't. The Microsoft bus mouse recognition was
  27. nonexistent before 0.98, the kernel just happily printed that a
  28. mouse was recognized.
  29.  
  30. >Obviously I can't run X until linux recognises my mouse. I tried the
  31. >mouse testing program in the X stuff, but I have no idea which device
  32. >my mouse would appear as. It's none of the serial devices.
  33.  
  34. The PS/2 mouse has major number 10, minor 1. Create the device with
  35.  
  36.     mknod /dev/psaux c 10 1
  37.  
  38. PS/2 mouse support for XFree86 is on the way, but not here yet. You'll
  39. need my mconv program to simulate a Microsoft serial mouse. I have
  40. written a new version of the program, which uses a fifo instead of a
  41. pseudo tty. The old version had the disadvantage that you had to
  42. statically specify a pty in Xconfig, a pty that could have been taken
  43. already by some other program.
  44.  
  45. There might still be some problems with the PS/2 mouse driver,
  46. unfortunately... Please report any problems.
  47.  
  48. Here's mconv.c. Compile with 'gcc -O -o mconv mconv.c'. You can start
  49. it up in /etc/rc.local, but don't forget to start it in the background
  50. if you do. I first forgot that... :-)
  51.  
  52. --
  53. Johan Myreen
  54. jem@cs.hut.fi
  55.  
  56. --- Cut here ---
  57.  
  58. /*
  59.  * Microsoft serial mouse emulator for PS/2 mouse using a named pipe.
  60.  *
  61.  * This program reads packets from a PS/2 mouse, transforms them 
  62.  * into the corresponding Microsoft serial mouse packets and writes
  63.  * them to a named pipe (fifo). Application programs not supporting
  64.  * the PS/2 mouse protocol (e.g. X11) can then read the packets from
  65.  * the fifo, which to them looks like a serial port with a Microsoft
  66.  * mouse on it.
  67.  *
  68.  * Create a named pipe with suitable permissions with mkfifo,
  69.  * for instance
  70.  *
  71.  *      mkfifo -m 666 /dev/mouse
  72.  * 
  73.  * Make sure you have an entry in /dev for the PS/2 pointing device.
  74.  * If not, create with
  75.  *
  76.  *      mknod /dev/psaux c 10 1
  77.  *
  78.  * Start up the conversion program with:
  79.  *
  80.  *      mconv /dev/psaux /dev/mouse &
  81.  *
  82.  * The program takes two arguments: the real mouse device and the
  83.  * name of the fifo.
  84.  *
  85.  * In Xconfig, fool X into thinking you have a Microsoft mouse
  86.  * on /dev/mouse (or whatever you called it):
  87.  *
  88.  * Microsoft "/dev/mouse"
  89.  * 
  90.  * Johan Myreen
  91.  * jem@cs.hut.fi
  92.  */
  93.  
  94.  
  95. #include <fcntl.h>
  96. #include <stdio.h>
  97. #include <signal.h>
  98. #include <errno.h>
  99.  
  100. static char *fifo_name, *prog_name;
  101. static int fifo, mouse;
  102.  
  103. void handler()
  104. {
  105.   fprintf(stderr, "\n%s: Mouse killed!\n", prog_name);
  106.   close(fifo);
  107.   close(mouse);
  108.   exit(0);
  109. }
  110.  
  111. void sigpipe_handler()
  112. {
  113.   close(fifo);
  114.   fifo = open(fifo_name, O_WRONLY);
  115.   if (fifo < 0) {
  116.     fprintf(stderr, "%s: Error reopening fifo.\n", prog_name);
  117.     close(mouse);
  118.     exit(1);
  119.   }
  120.   signal(SIGPIPE, sigpipe_handler);
  121. }
  122.   
  123.  
  124. unsigned char getbyte(void)
  125. {
  126.   static unsigned char buf[1024];
  127.   static unsigned char *bp = buf, *ep = buf;
  128.   int n;
  129.  
  130.   if (bp == ep) {
  131.     bp = ep = buf;
  132.     n = read(mouse, buf, 1024);
  133.     if (n>0) {
  134.       ep += n;
  135.     }
  136.   }
  137.   return *bp++;
  138. }
  139.  
  140.  
  141. void track_mouse(void)
  142. {
  143.   unsigned char byte1, byte2, byte3, out[3], outbyte;
  144.   int ret;
  145.  
  146.   while (1) {
  147.     byte1 = getbyte();
  148.     if (byte1 & 0xc0)
  149.       continue;                    /* Resynchronize */
  150.     byte2 = getbyte();
  151.     byte3 = getbyte();
  152.     byte3 = -byte3;
  153.     outbyte = 0x40;
  154.     outbyte |= ((byte2 >> 6)&0x03);
  155.     outbyte |= ((byte3 >> 4)&0x0c);
  156.     outbyte |= (byte1&0x01) << 5;    /* Left button */
  157.     outbyte |= (byte1&0x02) << 3;    /* Right button */
  158.     out[0] = outbyte;
  159.     out[1] = (byte2)&0x3f;
  160.     out[2] = (byte3)&0x3f;
  161.     ret = write(fifo, out, 3);
  162.     if (ret < 0 && errno != EPIPE)
  163.       return;
  164.   }
  165. }
  166.  
  167.  
  168. int main(int argc, char **argv)
  169. {
  170.  
  171.   prog_name = argv[0];
  172.   if (argc < 3) {
  173.     fprintf(stderr, "Usage: %s <mouse dev> <fifo>\n", argv[0]);
  174.     exit(1);
  175.   }
  176.  
  177.   mouse = open(argv[1], O_RDONLY);
  178.  
  179.   if (mouse < 0) {
  180.     fprintf(stderr, "%s: error %d opening mouse. Exiting.\n", prog_name, errno);
  181.     close(fifo);
  182.     exit(1);
  183.   }
  184.  
  185.   fifo_name = argv[2];
  186.   fifo = open(fifo_name, O_WRONLY);
  187.  
  188.   if (fifo < 0) {
  189.     fprintf(stderr, "%s: error %d opening fifo. Exiting.\n", prog_name, errno);
  190.     exit(1);
  191.   }
  192.  
  193.   signal(SIGTERM, handler);
  194.   signal(SIGINT, handler);
  195.   signal(SIGHUP, handler);
  196.   signal(SIGPIPE, sigpipe_handler);
  197.  
  198.   track_mouse();
  199.  
  200.   fprintf(stderr, "%s: error %d reading or writing.\n", prog_name, errno);
  201.   close(fifo);
  202.   close(mouse);
  203. }
  204.  
  205.  
  206.